package com.taptech.doufu.ui.viewholder;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.ui.activity.NewCpDesActivity;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.MainHomeActivity;
import com.taptech.doufu.bean.AlbumsBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.StartActivityUtil;
import com.taptech.doufu.util.TMAnalysis;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lsy on 2015/8/31.
 */
public class ImageSubjectViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{

    Context mContext;

    protected RelativeLayout mSubjectLayout;
    protected SimpleDraweeView mSubjectImageView;
    protected TextView mSubjectDescribtionView;

    final int PARENT_SCREEN_SPACE = ScreenUtil2.dip2px(8);
    final int IMAGE_SPACE = ScreenUtil2.dip2px(10);

    AlbumsBean albumsBean;

    public ImageSubjectViewHolder(Context context, View itemView)
    {
        super(itemView);
        mContext = context;
        mSubjectLayout = (RelativeLayout) itemView.findViewById(R.id.subject_layout);
        mSubjectImageView = (SimpleDraweeView) itemView.findViewById(R.id.subject_imageview);
        mSubjectDescribtionView = (TextView) itemView.findViewById(R.id.subject_describtion);
    }

    private void initUIData(AlbumsBean bean, int imageNum, SimpleDraweeView imageview, TextView titleTextView, int position)
    {
        // UI数据
        // 屏幕内最多展示3.5张图
        albumsBean = bean;
        double displayImageNum = imageNum < 4 ? imageNum : 3.5;

        int parentWidth = 0;
        int imageWidth = 0;

        //每个单项专题的尺寸
        RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams((int) (imageWidth), 100);

        if (imageNum == 1)
        {
            if ((bean.getIcon() != null)||(bean.getW_img()!=null))
            {

                // 图片宽度为屏宽减左右各5dp
                parentWidth = ScreenUtil2.getScreenWidthPixel((Activity) mContext) - PARENT_SCREEN_SPACE * 2;
                imageWidth = parentWidth;
                float imageHeight_scale;
                if (bean.isNovelSingel())
                {
                    imageHeight_scale = 3.0f;
                } else if (bean.isTagSingle())
                {
                    imageHeight_scale = 4.0f;
                } else
                {
                    imageHeight_scale = 2.0f;
                }
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageWidth, (int) (imageWidth / (imageHeight_scale)));

                mSubjectImageView.setLayoutParams(params);
                Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + "com.taptech.doufu" + R.drawable.biankuang_home_image_width);

                mSubjectImageView.setBackgroundResource(R.drawable.biankuang_home_image_width);
                mSubjectImageView.setImageURI(uri);
                if (bean.isTagSingle())
                {
                    if (bean.getW_img() != null)
                    {
                        ImageManager.loadImage(mSubjectImageView, bean.getW_img(), imageHeight_scale);
                    }
                } else
                {
                    if (bean.getIcon() != null)
                    {
                        ImageManager.loadImage(mSubjectImageView, bean.getIcon(), imageHeight_scale);
                    }
                }
                // 父容器宽高
                lparams = new RelativeLayout.LayoutParams(parentWidth, 100);
                // 图片与父容器没有间距
                lparams.setMargins(0, 0, 0, 0);
            }
            if (bean.getName() != null)
            {
                mSubjectDescribtionView.setText(bean.getName());
            }
            mSubjectDescribtionView.setVisibility(View.GONE);
        } else if (imageNum == 2)
        {
            if (bean.getIcon() != null)
            {
                // 图片宽度为屏宽减左右各5dp
                parentWidth = ScreenUtil2.getScreenWidthPixel((Activity) mContext) - PARENT_SCREEN_SPACE * 2;
                // 除去父控件的间隙，剩下的就是图片宽度
                imageWidth = (parentWidth - IMAGE_SPACE * (imageNum + 1)) / imageNum;
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageWidth, imageWidth / 2);
                mSubjectImageView.setLayoutParams(params);
                Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + "com.taptech.doufu" + R.drawable.biankuang_home_image_width);

                mSubjectImageView.setBackgroundResource(R.drawable.biankuang_home_image_width);
                if (bean.getIcon() != null)
                {
                    ImageManager.loadImage(mSubjectImageView, bean.getIcon(), 2.0f);
                }
                // 父容器宽高
                lparams = new RelativeLayout.LayoutParams(parentWidth / imageNum, 100);
                // 图片与父容器没有间距
                if (position == 0)
                {
                    lparams.setMargins(IMAGE_SPACE, 0, IMAGE_SPACE / 2, 0);
                } else if (position == imageNum - 1)
                {
                    lparams.setMargins(IMAGE_SPACE / 2, 0, IMAGE_SPACE, 0);
                } else
                {
                    lparams.setMargins(IMAGE_SPACE / 2, 0, IMAGE_SPACE / 2, 0);
                }
            }
            if (bean.getName() != null)
            {
                mSubjectDescribtionView.setText(bean.getName());
            }
        } else if (imageNum == 3)
        {
            if (bean.getIcon() != null)
            {
                // 图片宽度为屏宽
                parentWidth = ScreenUtil2.getScreenWidthPixel((Activity) mContext) - PARENT_SCREEN_SPACE * 2;
                // 除去父控件的间隙，剩下的就是图片宽度
                imageWidth = (int) ((parentWidth - IMAGE_SPACE * 4) / 3);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageWidth, imageWidth);

                lparams = new RelativeLayout.LayoutParams(parentWidth / imageNum, 100);
                // 图片与父容器没有间距
                if (position == 0)
                {
                    // 父容器宽高
                    //lparams = new RelativeLayout.LayoutParams((int) (imageWidth + PARENT_SCREEN_SPACE + IMAGE_SPACE * 1.5), 100);
                    params.setMargins(IMAGE_SPACE, 0, IMAGE_SPACE / 2, 0);
                } else if (position == imageNum - 1)
                {
                    // 父容器宽高
                    // lparams = new RelativeLayout.LayoutParams((int) (imageWidth + PARENT_SCREEN_SPACE + IMAGE_SPACE * 1.5), 100);
                    params.setMargins(IMAGE_SPACE / 2, 0, IMAGE_SPACE, 0);
                } else
                {
                    // 父容器宽高
                    // lparams = new RelativeLayout.LayoutParams(imageWidth + IMAGE_SPACE, 100);
                    params.setMargins(IMAGE_SPACE / 2, 0, IMAGE_SPACE / 2, 0);
                }
                mSubjectImageView.setLayoutParams(params);
                Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + "com.taptech.doufu/" + R.drawable.biankuang_home_image);

                mSubjectImageView.setBackgroundResource(R.drawable.biankuang_home_image);
                if (bean.getCover() != null)
                {
                    ImageManager.loadImage(mSubjectImageView, bean.getCover(), 1.0f);
                }
            }
            if (bean.getName() != null)
            {
                mSubjectDescribtionView.setText(bean.getName());
            }
        } else if (imageNum > 3)
        {

            if (bean.getIcon() != null)
            {
                // 图片宽度为屏宽
                parentWidth = ScreenUtil2.getScreenWidthPixel((Activity) mContext);
                // 除去父控件的间隙，剩下的就是图片宽度
                imageWidth = (int) ((parentWidth - IMAGE_SPACE * 4 - PARENT_SCREEN_SPACE) / 3.5);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageWidth, imageWidth);


                // 图片与父容器没有间距
                if (position == 0)
                {
                    // 父容器宽高
                    lparams = new RelativeLayout.LayoutParams((int) (imageWidth + PARENT_SCREEN_SPACE + IMAGE_SPACE * 1.5), 100);
                    params.setMargins(IMAGE_SPACE + PARENT_SCREEN_SPACE, 0, IMAGE_SPACE / 2, 0);
                } else if (position == imageNum - 1)
                {
                    // 父容器宽高
                    lparams = new RelativeLayout.LayoutParams((int) (imageWidth + PARENT_SCREEN_SPACE + IMAGE_SPACE * 1.5), 100);
                    params.setMargins(IMAGE_SPACE / 2, 0, IMAGE_SPACE + PARENT_SCREEN_SPACE, 0);
                } else
                {
                    // 父容器宽高
                    lparams = new RelativeLayout.LayoutParams(imageWidth + IMAGE_SPACE, 100);
                    params.setMargins(IMAGE_SPACE / 2, 0, IMAGE_SPACE / 2, 0);
                }
                mSubjectImageView.setLayoutParams(params);
                Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + "com.taptech.doufu/" + R.drawable.biankuang_home_image);

                mSubjectImageView.setBackgroundResource(R.drawable.biankuang_home_image);
                if (bean.getCover() != null)
                {
                    ImageManager.loadImage(mSubjectImageView, bean.getCover(), 1.0f);
                }
            }
            if (bean.getName() != null)
            {
                mSubjectDescribtionView.setText(bean.getName());
            }
        }

        mSubjectLayout.setLayoutParams(lparams);
        mSubjectLayout.setTag(position);
        mSubjectLayout.setOnClickListener(this);
    }

    public void setViewHolderData(AlbumsBean bean, int imageNum, int position)
    {
        if (bean == null || imageNum <= 0)
        {
            return;
        }
        initUIData(bean, imageNum, mSubjectImageView, mSubjectDescribtionView, position);


    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.subject_layout:
                if (albumsBean != null && view != null)
                {
                    if (albumsBean.isTagSingle())
                    {
                        Intent intent = new Intent(mContext, NewCpDesActivity.class);
                        intent.putExtra(Constant.TOPIC_ID, albumsBean.getId());
                        intent.putExtra(Constant.TYPE, NewCpDesActivity.TYPE_TAG);
                        mContext.startActivity(intent);
                    } else
                    {
                        StartActivityUtil.albumsOnclick(mContext, albumsBean);
                    }
                    try
                    {
                        Map<String, String> analysisMap = new HashMap<String, String>();
                        String eventName = "";
                        Activity activity = (Activity) mContext;
                        if (activity instanceof MainHomeActivity)
                        {
                            eventName = "home-single-subject";
                        }
                        analysisMap.put("mid", albumsBean.getId());
                        analysisMap.put("mposition", view.getTag() + "");
                        if (albumsBean.getObject_type() != null)
                        {
                            analysisMap.put("mtype", albumsBean.getObject_type());
                        }
                        TMAnalysis.event(mContext, eventName, analysisMap);
                    } catch (Exception e)
                    {   //说明是ApplicationContext
                        e.printStackTrace();
                    }
                }
                break;
        }
    }
}
